﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using RanchBuddy.Core.Services.Impl;
using StructureMap;

namespace RanchBuddy.Core.Services.Impl
{
    [Pluggable("Default")]
    public class ConfigurationService : IConfigurationService
    {
        public static int GetDefaultCacheDuration_Days()
        {
            return Convert.ToInt32(ConfigurationManager.AppSettings["DefaultCacheDuration_Days"]);
        }
        public static int GetDefaultCacheDuration_Hours()
        {
            return Convert.ToInt32(ConfigurationManager.AppSettings["DefaultCacheDuration_Hours"]);
        }
        public static int GetDefaultCacheDuration_Minutes()
        {
            return Convert.ToInt32(ConfigurationManager.AppSettings["DefaultCacheDuration_Minutes"]);
        }
        public static string GetLogFileDirectory()
        {
            return ConfigurationManager.AppSettings["LogFileDirectory"].ToString();
        }
        public static LoggingType GetLoggingType()
        {
            string loggingType = ConfigurationManager.AppSettings["LoggingType"].ToString();
            if(loggingType.ToLower() == "verbose")
            {
                return LoggingType.Verbose;
            }
            else if (loggingType.ToLower() == "error")
            {
                return LoggingType.Error;
            }
            return LoggingType.Error;
        }
        public static string GetRoot()
        {
            string result = "";
            if(ConfigurationManager.AppSettings["Root"] != null)
            {
                result = ConfigurationManager.AppSettings["Root"].ToString();
            }
            return result;
        }
        public static string Salt
        {
            get { return "tm-es@as"; }
        }
        public enum MasterTemplateTypes
        {
            Site,
            RanchBuddy
        }

        public enum ThemeTypes
        {
            Default,
            Blank
        }
        public static string GetRegExDateValidation()
        {
            string result = "";
            if(ConfigurationManager.AppSettings["ValidationDateFormatRegEx"] != null)
            {
                result = ConfigurationManager.AppSettings["ValidationDateFormatRegEx"].ToString();
            }
            return result;
        }
        public static string GetRegExUsernameComplexityValidation()
        {
            string result = "";
            if (ConfigurationManager.AppSettings["ValidationUsernameComplexityRegEx"] != null)
            {
                result = ConfigurationManager.AppSettings["ValidationUsernameComplexityRegEx"].ToString();
            }
            return result;
        }
        public static string GetRegExPasswordComplexityValidation()
        {
            string result = "";
            if(ConfigurationManager.AppSettings["ValidationPasswordComplexityRegEx"] != null)
            {
                result = ConfigurationManager.AppSettings["ValidationPasswordComplexityRegEx"].ToString();
            }
            return result;
        }
        public static string GetRegExEmailValidation()
        {
            string result = "";
            if (ConfigurationManager.AppSettings["ValidationEmailRegEx"] != null)
            {
                result = ConfigurationManager.AppSettings["ValidationEmailRegEx"].ToString();
            }
            return result;
        }
        public static int GetMinimumPasswordLength()
        {
            int result = 0;
            if(ConfigurationManager.AppSettings["MinimumPasswordLength"] != null)
            {
                result = Convert.ToInt32(ConfigurationManager.AppSettings["MinimumPasswordLength"]);
            }
            return result;
        }
        public static int GetMaximumPasswordLength()
        {
            int result = 0;
            if (ConfigurationManager.AppSettings["MaximumPasswordLength"] != null)
            {
                result = Convert.ToInt32(ConfigurationManager.AppSettings["MaximumPasswordLength"]);
            }
            return result;
        }
        public static int GetMinimumUsernameLength()
        {
            int result = 0;
            if (ConfigurationManager.AppSettings["MinimumUsernameLength"] != null)
            {
                result = Convert.ToInt32(ConfigurationManager.AppSettings["MinimumUsernameLength"]);
            }
            return result;
        }
        public static int GetMaximumUsernameLength()
        {
            int result = 0;
            if (ConfigurationManager.AppSettings["MaximumUsernameLength"] != null)
            {
                result = Convert.ToInt32(ConfigurationManager.AppSettings["MaximumUsernameLength"]);
            }
            return result;
        }
        public static ThemeTypes ThemeToUse()
        {
            ThemeTypes result = ThemeTypes.Blank;

            if (ConfigurationManager.AppSettings["ThemeToUse"] != null)
            {
                if (ConfigurationManager.AppSettings["ThemeToUse"].ToString().ToLower() == "default")
                    result = ThemeTypes.Default;
                else
                    result = ThemeTypes.Blank;
            }
            return result;
        }

        public static MasterTemplateTypes MasterTemplateToUse()
        {
            MasterTemplateTypes result = MasterTemplateTypes.Site;

            if(ConfigurationManager.AppSettings["MasterTemplateToUse"] != null)
            {
                if(ConfigurationManager.AppSettings["MasterTemplateToUse"].ToString().ToLower() == "ranchbuddy")
                    result = MasterTemplateTypes.RanchBuddy;
            }

            return result;
        }

        public static string GetFromEmailAddress()
        {
            string result = "";
            if(ConfigurationManager.AppSettings["FromEmailAddress"] != null)
            {
                result = ConfigurationManager.AppSettings["FromEmailAddress"].ToString();
            }
            return result;
        }

        public static string GetSiteName()
        {
            string result = "";
            if(ConfigurationManager.AppSettings["SiteName"] != null)
            {
                result = ConfigurationManager.AppSettings["SiteName"].ToString();
            }
            return result;
        }

        public static int GetImageWidthThumbnail()
        {
            int result = 0;
            if (ConfigurationManager.AppSettings["ImageWidthThumbnail"] != null)
            {
                try
                {
                    result = Convert.ToInt32(ConfigurationManager.AppSettings["ImageWidthThumbnail"]);
                }
                catch (Exception)
                {
                    result = 0;
                }
            }
            return result;
        }

        public static int GetImageWidthSmall()
        {
            int result = 0;
            if (ConfigurationManager.AppSettings["ImageWidthSmall"] != null)
            {
                try
                {
                    result = Convert.ToInt32(ConfigurationManager.AppSettings["ImageWidthSmall"]);
                }
                catch (Exception)
                {
                    result = 0;
                }
            }
            return result;
        }

        public static int GetImageWidthMedium()
        {
            int result = 0;
            if (ConfigurationManager.AppSettings["ImageWidthMedium"] != null)
            {
                try
                {
                    result = Convert.ToInt32(ConfigurationManager.AppSettings["ImageWidthMedium"]);
                }
                catch (Exception)
                {
                    result = 0;
                }
            }
            return result;
        }

        public static int GetImageWidthLarge()
        {
            int result = 0;
            if (ConfigurationManager.AppSettings["ImageWidthLarge"] != null)
            {
                try
                {
                    result = Convert.ToInt32(ConfigurationManager.AppSettings["ImageWidthLarge"]);
                }
                catch (Exception)
                {
                    result = 0;
                }
            }
            return result;
        }

        public static string GetAccountStatsImagesDirectory()
        {
            string result = "";
            if(ConfigurationManager.AppSettings["AccountStatsImagesDirectory"] != null)
            {
                result = ConfigurationManager.AppSettings["AccountStatsImagesDirectory"].ToString();
            }
            return result;
        }
        public static string GetRanchBuddyEfConnectionString()
        {
            return ConfigurationManager.ConnectionStrings["RanchBuddyEfConnectionString"].ToString();
        }
        public static string GetRanchBuddyLsConnectionString()
        {
            return ConfigurationManager.ConnectionStrings["RanchBuddyLsConnectionString"].ToString();
        }
    }
}